package com.ruc.deke.utils;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * 发送通知工具类
 * 
 * @author: cyj
 * @date: 2017年4月24日 下午2:39:18
 */
public class NotificationUtil {

	static int port = 25;

	static String server = "smtp.163.com";// 代理服务器

	static String from = "rucdbcourse@163.com";

	static String user = "rucdbcourse@163.com";

	static String password = "dbcourse123";// 代理授权码

	/**
	 * 发送邮件
	 * 
	 * @param email
	 *            目标地址
	 * @param subject
	 *            主题
	 * @param body
	 *            内容
	 * @author: cyj
	 * @date: 2017年4月24日 下午2:39:50
	 */
	public static void sendEmail(String email, String subject, String body) {
		try {
			Properties props = new Properties();
			props.put("mail.smtp.host", server);
			props.put("mail.smtp.port", String.valueOf(port));
			props.put("mail.smtp.auth", "true");
			Transport transport = null;
			Session session = Session.getDefaultInstance(props, null);
			transport = session.getTransport("smtp");
			transport.connect(server, user, password);
			MimeMessage msg = new MimeMessage(session);
			msg.setSentDate(new Date());
			InternetAddress fromAddress = new InternetAddress(from);
			msg.setFrom(fromAddress);
			InternetAddress[] toAddress = new InternetAddress[1];
			toAddress[0] = new InternetAddress(email);
			msg.setRecipients(Message.RecipientType.TO, toAddress);
			msg.setSubject(subject, "UTF-8");
			msg.setText(body, "UTF-8");
			msg.saveChanges();
			transport.sendMessage(msg, msg.getAllRecipients());
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
}